package com.iemes.controller.forming;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.CheckContentFormMap;
import com.iemes.entity.MoldingInspectionFormMap;
import com.iemes.entity.PretendCheckFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.forming.PretendCheckMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;
import com.iemes.util.DateUtils;
@Controller
@RequestMapping("inspection")
public class PretendCheckController extends BaseController{
	@Inject
	private PretendCheckMapper pretendCheckMapper;
	
	
	@RequestMapping(value = "assembly", method = RequestMethod.GET, produces = "text/html; charset=utf-8")
	public String examinepru(Model model) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		//如果不是主管
		if (userFormMap.get("site").equals("0")) {
			//查询装拆模点检项目 
			MoldingInspectionFormMap findbyFrist = baseMapper.findbyFrist("Department", "2", MoldingInspectionFormMap.class);
			//再查询点检内容
			List<CheckContentFormMap> findByAttribute = baseMapper.findByAttribute("InspectionId", findbyFrist.get("id").toString(), CheckContentFormMap.class);
			model.addAttribute("molding", findbyFrist);
			model.addAttribute("checkContent", findByAttribute);
			//进入页面操作
			return Common.BACKGROUND_PATH + "/forming/inspection/assembly_regulatorAdd";		
		}
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/forming/inspection/assembly_list";	
	}
	
	@ResponseBody
	@RequestMapping("assembly_findByPage")
	public PageView findByPage(String pageNow, String pageSize) {
		PretendCheckFormMap pretendCheckFormMap = getFormMap(PretendCheckFormMap.class);
		List<Map<String, Object>> findByPage = pretendCheckMapper.getPretendCheck(pretendCheckFormMap);
		try {
			LuogicPaging(pageNow, pageSize, findByPage);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return pageView;
	}
	
	@RequestMapping("assembly_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		PretendCheckFormMap findbyFrist = pretendCheckMapper.findbyFrist("id", id, PretendCheckFormMap.class);
		model.addAttribute("progress", findbyFrist);	
		return Common.BACKGROUND_PATH + "/forming/progress/statistics_edit";
	}

	@ResponseBody
	@RequestMapping(value = "assembly_editEntity")
	@SystemLog(module = "成型管理", methods = "装拆模检查-修改装拆模检查")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity() throws Exception {
		PretendCheckFormMap pretendCheckFormMap = getFormMap(PretendCheckFormMap.class);
		pretendCheckMapper.editEntity(pretendCheckFormMap);		
		return "success";
	}
	@ResponseBody
	@RequestMapping(value = "assembly_deleteEntity")
	@SystemLog(module = "成型管理", methods = "装拆模检查-删除装拆模检查")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		String[] ids = getParaValues("ids");
		String arr = ids[0];
		String[] str = arr.split(",");
		PretendCheckFormMap pretendCheckFormMap = new PretendCheckFormMap();
		for (int i = 0; i < str.length; i++) {
			pretendCheckFormMap.put("Status", 2);
			pretendCheckFormMap.put("id", str[i]);
			pretendCheckMapper.editEntity(pretendCheckFormMap);
		}
		return "success";
	}
	
	
	@ResponseBody
	@RequestMapping(value = "assembly_addEntity")
	@SystemLog(module = "成型管理", methods = "装拆模检查-增加装拆模检查")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity() throws Exception {
		PretendCheckFormMap pretendCheckFormMap = getFormMap(PretendCheckFormMap.class);
		pretendCheckFormMap.put("Record_Date", DateUtils.getStringDateTime());
		pretendCheckFormMap.put("Status", 1);	
		pretendCheckMapper.addEntity(pretendCheckFormMap);		
		return "success";
	}
}
